import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import VueSetupExtend from "vite-plugin-vue-setup-extend";
// import { nodePolyfills } from "vite-plugin-node-polyfills";
import { resolve } from "path";

// import dynamicImport from "vite-plugin-dynamic-import";
import { viteCommonjs } from "@originjs/vite-plugin-commonjs";

const pathResolve = dir => {
	return resolve(__dirname, ".", dir);
};

const alias = {
	"@": pathResolve("src")
};

// https://vite.dev/config/
// export default defineConfig({
//   plugins: [vue()],
// })

export default defineConfig(({ command, mode }) => {
	// 获取.env文件里定义的环境变量
	const env = loadEnv(mode, process.cwd());
	//console.log(env);   //变量在命令行里打印出来
	//.env文件中的环境变量必须以VITE_为前缀，否则无法引用成功
	const { VITE_BASE, VITE_APP_ENV } = env;

	const isProduction = mode === "production";
	const isDevelopment = mode === "development";

	console.log("当前环境:", mode);
	console.log("VITE_BASE:", VITE_BASE);
	console.log("VITE_APP_ENV:", VITE_APP_ENV);

	// 基础配置
	const baseConfig = {
		base: VITE_BASE,
		assetsInclude: ["**/*.wasm"],
		plugins: [vue(), VueSetupExtend(), viteCommonjs()],
		optimizeDeps: {
			exclude: [
				"@cornerstonejs/dicom-image-loader",
				"@cornerstonejs/polymorphic-segmentation"
			],
			include: [
				"dicom-parser",
				"gl-matrix",
				"@kitware/vtk.js",
				"d3-array",
				"d3-interpolate"
			]
		},
		worker: {
			format: "es",
			rollupOptions: {
				external: ["@icr/polyseg-wasm"]
			}
		},
		build: {
			outDir: "dist",
			emptyOutDir: true,
			// 生产环境优化配置
			minify: "terser",
			sourcemap: isProduction ? false : true,
			chunkSizeWarningLimit: 1000,
			rollupOptions: {
				output: {
					manualChunks: {
						vendor: ["vue", "vue-router", "pinia", "vue-i18n"],
						ui: ["element-plus", "ant-design-vue"],
						dicom: [
							"@cornerstonejs/core",
							"@cornerstonejs/tools",
							"@cornerstonejs/dicom-image-loader"
						],
						vtk: ["@kitware/vtk.js"],
						utils: ["axios", "mitt", "crypto-js"]
					},
					chunkFileNames: "assets/[name]-[hash].js",
					entryFileNames: "assets/[name]-[hash].js",
					assetFileNames: "assets/[name]-[hash].[ext]"
				}
			},
			terserOptions: {
				compress: {
					drop_console: isProduction,
					drop_debugger: isProduction
				}
			}
		},
		resolve: {
			alias,
			extensions: [".vue", ".js"]
		},
		server: {
			host: "0.0.0.0",
			port: 4000,
			proxy: isDevelopment
				? {
						"/api": {
							target: "http://10.168.128.101:8099", // 开发环境应用服务器
							changeOrigin: true,
							rewrite: path => path.replace(/^\/api/, ""),
							secure: false
						},
						"/v3/api": {
							target: "http://10.168.128.101:8099", // 开发环境应用服务器 - v3接口
							changeOrigin: true,
							secure: false
						},
						"/files": {
							target: "http://10.168.128.102:7002", // 开发环境DICOM文件服务器
							changeOrigin: true,
							rewrite: path => path.replace(/^\/files/, ""),
							secure: false
						}
				  }
				: undefined
		},
		// 全局变量定义
		define: {
			__APP_ENV__: JSON.stringify(mode),
			__BUILD_TIME__: JSON.stringify(new Date().toISOString())
		}
	};

	return baseConfig;
});

// export default defineConfig({
// 	assetsInclude: ["**/*.wasm"],
// 	plugins: [vue(), VueSetupExtend(), viteCommonjs()],
// 	optimizeDeps: {
// 		exclude: [
// 			"@cornerstonejs/dicom-image-loader",
// 			"@cornerstonejs/polymorphic-segmentation"
// 		],
// 		include: ["dicom-parser"]
// 	},
// 	worker: {
// 		format: "es",
// 		rollupOptions: {
// 			external: ["@icr/polyseg-wasm"]
// 		}
// 	},
// 	build: {
// 		outDir: "webdicom",
// 		emptyOutDir: true
// 	},
// 	resolve: {
// 		alias: {
// 			"@": "/src"
// 		},
// 		extensions: [".vue", ".js"]
// 	},
// 	server: {
// 		host: "0.0.0.0",
// 		port: 4000
// 	}
// });
